package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.StockOutOrder;
import com.ruoyi.factorydata.service.IStockOutOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库单Controller
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/warehouse/stockOutOrder")
public class StockOutOrderController extends BaseController
{
    @Autowired
    private IStockOutOrderService stockOutOrderService;

    /**
     * 查询出库单列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockOutOrder stockOutOrder)
    {
        startPage();
        List<StockOutOrder> list = stockOutOrderService.selectStockOutOrderList(stockOutOrder);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:export')")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockOutOrder stockOutOrder)
    {
        List<StockOutOrder> list = stockOutOrderService.selectStockOutOrderList(stockOutOrder);
        ExcelUtil<StockOutOrder> util = new ExcelUtil<StockOutOrder>(StockOutOrder.class);
        util.exportExcel(response, list, "出库单数据");
    }

    /**
     * 获取出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockOutOrderService.selectStockOutOrderById(id));
    }

    /**
     * 新增出库单
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:add')")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockOutOrder stockOutOrder)
    {
        return toAjax(stockOutOrderService.insertStockOutOrder(stockOutOrder));
    }


    /**
     * 修改出库单
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:edit')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockOutOrder stockOutOrder)
    {
        return toAjax(stockOutOrderService.updateStockOutOrder(stockOutOrder));
    }

    /**
     * 删除出库单
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:remove')")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockOutOrderService.deleteStockOutOrderByIds(ids));
    }

    /**
     * 审核领料单
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:process')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping("auth")
    public AjaxResult authRequest(@RequestBody StockOutOrder stockOutOrder) {
        return toAjax(stockOutOrderService.authRequest(stockOutOrder));
    }

    /**
     * 拒绝领料单
     */
    @PreAuthorize("@ss.hasPermi('warehouse:stockOutOrder:process')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping("refuse")
    public AjaxResult refuseRequest(@RequestBody StockOutOrder stockOutOrder) {
        return toAjax(stockOutOrderService.refuseRequest(stockOutOrder));
    }
}
